Mosaicism [B]
Memory limit: 64 MB
Molecular biologists have been studying genomes (i.e. sequences of genes) of
particular organisms, trying to infer some conclusions about
evolution of species and operation of cells and tissues.
In their research they compare the structure and functions of respective
genes and they identify genes very similar to each other (called
homologous genes).
Recently, while examining viruses attacking bacteria (called bacteriophages
or simply phages), they observed a very intriguing phenomenon.
If sequences of genes of some phages are arranged one below another
and homologous genes are coloured with the same colour, then a peculiar
mosaic is obtained:
To describe the phenomenon that they discovered, scientists invented a measure
that they called the coefficient of mosaicism.
This coefficient can be computed for a phage only in juxtaposition with
some other phages - it is then equal to the total number of points computed
in the following way.
For every two genes from the -th phage and for every two different phages such that:
- gene has some homologous gene in the -th phage
and does not have homologous genes in -th phage,
- gene has some homologous gene in the -th phage
and does not have homologous genes in the -th phage,
the
-th phage (
and
) receives
point.
Each quadruple
is counted exactly once in the summation,
i.e. quadruples
and
are considered the same.
In the situation illustrated in the above figure, phage number has the coefficient
of mosaicism equal to .
That is because the blue gene has a homologous gene in phage and does not have any in
phage ; on the other hand, the orange gene has a homologous gene in phage and does not have
any in phage .
A similar situation holds for genes: brown and orange.
Phage number has the coefficient of mosaicism equal to - it receives
points for pairs of genes: red-blue, red-brown, 2 times yellow-blue and
2 times yellow-brown.
Manual computation of the coefficients is quite difficult, so the biologists
decided to ask you to write a program that would compute the coefficients of mosaicism
of all given phages.
Task
Write a program that:
- reads the descriptions of pairs of homologous genes in a set of phages
from the standard input,
- computes the coefficients of mosaicism for all phages,
- writes the result to the standard output.
Input
The first line of input contains one integer
() denoting the number of phages under consideration.
The -th of the following lines contains a description of the sequence
of genes of the -th phage.
Each such description starts with a single integer
() denoting the number of genes in the -th phage's sequence,
which is followed by integers
() describing its successive genes.
All these numbers are separated with single spaces.
Two genes are considered homologous if and only if they are represented
by equal numbers in the description from the input.
The scientists proved that no two genes of a phage can be homologous1,
so no number can appear an a description of a single phage twice.
Output
The output should consist of lines.
-th of them should contain a single integer equal to the coefficient of mosaicism
of the -th phage.
Example
For the input data:
4
4 1 5 2 4
3 3 6 4
3 5 2 6
3 4 3 1
the correct result is:
6
3
2
1
The figure from the task description illustrates the example test case.
1. In reality, appearance of two homologous genes in a single phage is quite improbable.
Task author: Aleksander Mielczarek.